#Makefile gero
C_CC=/usr/bin/ccache ppu-gcc
MKLIB= ocamlmklib
C_CFLAGS=-m32  -mabi=altivec -maltivec
C_INCLUDES=-I/usr/local/lib/ocaml/
ML_LDFLAGS=unix.cmxa -thread threads.cmxa bigarray.cmxa
C_SRC=gero.c
ML_SRC=gero.ml
C_OBJ= $(C_SRC:.c=.o)
ML_OBJ=$(ML_SRC:.ml=.cmx)
OBJS=$(C_OBJ) $(ML_OBJ)
CC_OBJS=$(C_OBJ) $(ML_SRC:.ml=.cmo)

gero.cmxa : clean $(OBJS)
	cd ./spu && make lib
	$(MKLIB) -custom  -cclib -lbigarray -lsimdmath -ocamlopt ocamlopt  -linkall -o gero $(OBJS)
	rm gero.o
	$(C_CC)  $(C_INCLUDES) -c gero.c $(C_CFLAGS)
	ppu-ar r gero.a $(C_OBJ)



top: install $(CC_OBJS)
	cd ./spu && make lib
	$(MKLIB) -custom  -cclib -lbigarray -lsimdmath -ocamlc ocamlc  -linkall -o gero $(CC_OBJS)
	rm gero.o
	$(C_CC)  $(C_INCLUDES) -c gero.c $(C_CFLAGS)
	ppu-ar r gero.a $(C_OBJ)
	ocamlmktop  bigarray.cma  unix.cma /usr/local/lib/ocaml/threads/threads.cma -cclib -lthreads -cclib -lunix -cclib -lpthread -cclib /usr/local/lib/ocaml/gero.a -cclib /usr/local/lib/ocaml/libgero.a -cclib /usr/lib/libspe2.so -cclib ./spu/fmaf4_spu.a gero.cma -o duracell

%cmo: %ml
	ocamlc -thread -custom threads.cma -c $< -cclib -lthreads -cclib -lunix -cclib -lpthread -cclib -lbigarray
%cmx: %ml
	ocamlopt -c $(ML_LDFLAGS) $< -cclib -lthreads -cclib -lunix -cclib -lpthread -cclib -lbigarray


%o: %c
	$(C_CC)  $(C_INCLUDES) -c -o $@ $< $(C_CFLAGS)

test: install
	cd ../../examples/fma_ppu && make clean && make
	../../examples/fma_ppu/ppu/fma_ppu

install: gero.cmxa
	cp gero.cmx /usr/local/lib/ocaml/
	cp gero.cmxa /usr/local/lib/ocaml/
	cp gero.cmi /usr/local/lib/ocaml/
	cp gero.a /usr/local/lib/ocaml/
	cp libgero.a /usr/local/lib/ocaml/
	ranlib /usr/local/lib/ocaml/gero.a
	ranlib /usr/local/lib/ocaml/libgero.a
	rm -rf /usr/local/lib/ocaml/spu
	mkdir -p /usr/local/lib/ocaml/spu
	cp ./spu/*.a /usr/local/lib/ocaml/spu/

clean:
	rm -rf *.o *.cm* *.a *.cmxa *~ *.so duracell
	cd ./spu && make clean
